home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 7: Programming / CDAT7.iso / demos / VisualAge for Java 2.0 Entry / setup / data1.cab / ide-e / IDE / cache / 1V04LGQ (.txt) < prev    next >
Encoding:
Java Class File  |  1998-09-16  |  15.0 KB  |  586 lines

  1. package com.sun.java.swing.preview;
  2.  
  3. import com.sun.java.accessibility.Accessible;
  4. import com.sun.java.swing.Icon;
  5. import com.sun.java.swing.JComponent;
  6. import com.sun.java.swing.JDialog;
  7. import com.sun.java.swing.SwingUtilities;
  8. import com.sun.java.swing.UIManager;
  9. import com.sun.java.swing.event.EventListenerList;
  10. import com.sun.java.swing.plaf.FileChooserUI;
  11. import com.sun.java.swing.preview.filechooser.FileFilter;
  12. import com.sun.java.swing.preview.filechooser.FileSystemView;
  13. import com.sun.java.swing.preview.filechooser.FileView;
  14. import java.awt.BorderLayout;
  15. import java.awt.Component;
  16. import java.awt.Container;
  17. import java.awt.Frame;
  18. import java.awt.event.ActionEvent;
  19. import java.awt.event.ActionListener;
  20. import java.io.File;
  21. import java.util.Vector;
  22.  
  23. public class JFileChooser extends JComponent implements Accessible {
  24.    public static final int OPEN_DIALOG = 0;
  25.    public static final int SAVE_DIALOG = 1;
  26.    public static final int CUSTOM_DIALOG = 2;
  27.    public static final int CANCEL_OPTION = 1;
  28.    public static final int APPROVE_OPTION = 0;
  29.    public static final int ERROR_OPTION = 0;
  30.    public static final int FILES_ONLY = 0;
  31.    public static final int DIRECTORIES_ONLY = 1;
  32.    public static final int FILES_AND_DIRECTORIES = 2;
  33.    public static final String CANCEL_SELECTION = "CancelSelection";
  34.    public static final String APPROVE_SELECTION = "ApproveSelection";
  35.    public static final String APPROVE_BUTTON_TEXT_CHANGED_PROPERTY = "ApproveButtonTextChangedProperty";
  36.    public static final String APPROVE_BUTTON_TOOL_TIP_TEXT_CHANGED_PROPERTY = "ApproveButtonToolTipTextChangedProperty";
  37.    public static final String DIRECTORY_CHANGED_PROPERTY = "directoryChanged";
  38.    public static final String SELECTED_FILE_CHANGED_PROPERTY = "ApproveSelection";
  39.    public static final String MULTI_SELECTION_ENABLED_CHANGED_PROPERTY = "fileFilterChanged";
  40.    public static final String FILE_SYSTEM_VIEW_CHANGED_PROPERTY = "FileSystemViewChanged";
  41.    public static final String FILE_VIEW_CHANGED_PROPERTY = "fileViewChanged";
  42.    public static final String FILE_HIDING_CHANGED_PROPERTY = "FileHidingChanged";
  43.    public static final String FILE_FILTER_CHANGED_PROPERTY = "fileFilterChanged";
  44.    public static final String FILE_SELECTION_MODE_CHANGED_PROPERTY = "fileSelectionChanged";
  45.    public static final String ACCESSORY_CHANGED_PROPERTY = "AccessoryChangedProperty";
  46.    public static final String DIALOG_TYPE_CHANGED_PROPERTY = "DialogTypeChangedProperty";
  47.    public static final String CHOOSABLE_FILE_FILTER_CHANGED_PROPERTY = "ChoosableFileFilterChangedProperty";
  48.    private String dialogTitle;
  49.    private String approveButtonText;
  50.    private String approveButtonToolTipText;
  51.    private ActionListener actionListener;
  52.    private Vector filters;
  53.    private JDialog dialog;
  54.    private int dialogType;
  55.    private int returnValue;
  56.    private JComponent accessory;
  57.    private FileView fileView;
  58.    private FileView uiFileView;
  59.    private boolean useFileHiding;
  60.    private int fileSelectionMode;
  61.    private boolean multiSelectionEnabled;
  62.    private FileFilter fileFilter;
  63.    private FileSystemView fileSystemView;
  64.    private File currentDirectory;
  65.    private File selectedFile;
  66.    private File[] selectedFiles;
  67.    static Class class$java$awt$event$ActionListener;
  68.    static Class class$java$awt$Frame;
  69.  
  70.    public JFileChooser() {
  71.       this((File)null);
  72.    }
  73.  
  74.    public JFileChooser(File directory) {
  75.       this.dialogTitle = null;
  76.       this.approveButtonText = null;
  77.       this.approveButtonToolTipText = null;
  78.       this.actionListener = null;
  79.       this.filters = new Vector(5);
  80.       this.dialog = null;
  81.       this.dialogType = 0;
  82.       this.returnValue = 0;
  83.       this.accessory = null;
  84.       this.fileView = null;
  85.       this.uiFileView = null;
  86.       this.useFileHiding = true;
  87.       this.fileSelectionMode = 0;
  88.       this.multiSelectionEnabled = false;
  89.       this.fileFilter = null;
  90.       this.fileSystemView = null;
  91.       this.currentDirectory = null;
  92.       this.selectedFile = null;
  93.       this.init();
  94.       this.setCurrentDirectory(directory);
  95.       this.setFileFilter(this.getAcceptAllFileFilter());
  96.    }
  97.  
  98.    public JFileChooser(String path) {
  99.       this.dialogTitle = null;
  100.       this.approveButtonText = null;
  101.       this.approveButtonToolTipText = null;
  102.       this.actionListener = null;
  103.       this.filters = new Vector(5);
  104.       this.dialog = null;
  105.       this.dialogType = 0;
  106.       this.returnValue = 0;
  107.       this.accessory = null;
  108.       this.fileView = null;
  109.       this.uiFileView = null;
  110.       this.useFileHiding = true;
  111.       this.fileSelectionMode = 0;
  112.       this.multiSelectionEnabled = false;
  113.       this.fileFilter = null;
  114.       this.fileSystemView = null;
  115.       this.currentDirectory = null;
  116.       this.selectedFile = null;
  117.       this.init();
  118.       this.setCurrentDirectory(this.getFileSystemView().createFileObject(path));
  119.       this.setFileFilter(this.getAcceptAllFileFilter());
  120.    }
  121.  
  122.    public boolean accept(File f) {
  123.       boolean shown = true;
  124.       if (this.fileFilter != null) {
  125.          shown = this.fileFilter.accept(f);
  126.       }
  127.  
  128.       return shown;
  129.    }
  130.  
  131.    public void addActionListener(ActionListener l) {
  132.       EventListenerList var10000 = super.listenerList;
  133.       Class var10001 = class$java$awt$event$ActionListener;
  134.       if (var10001 == null) {
  135.          try {
  136.             var10001 = Class.forName("java.awt.event.ActionListener");
  137.          } catch (ClassNotFoundException var2) {
  138.             throw new NoClassDefFoundError(((Throwable)var2).getMessage());
  139.          }
  140.  
  141.          class$java$awt$event$ActionListener = var10001;
  142.       }
  143.  
  144.       var10000.add(var10001, l);
  145.    }
  146.  
  147.    public void addChoosableFileFilter(FileFilter filter) {
  148.       if (!this.filters.contains(filter)) {
  149.          FileFilter[] oldValue = this.getChoosableFileFilters();
  150.          this.filters.addElement(filter);
  151.          ((JComponent)this).firePropertyChange("ChoosableFileFilterChangedProperty", oldValue, this.getChoosableFileFilters());
  152.       }
  153.  
  154.    }
  155.  
  156.    public void approveSelection() {
  157.       this.returnValue = 0;
  158.       if (this.dialog != null) {
  159.          this.dialog.setVisible(false);
  160.       }
  161.  
  162.       this.fireActionPerformed("ApproveSelection");
  163.    }
  164.  
  165.    public void cancelSelection() {
  166.       this.returnValue = 1;
  167.       if (this.dialog != null) {
  168.          this.dialog.setVisible(false);
  169.       }
  170.  
  171.       this.fireActionPerformed("CancelSelection");
  172.    }
  173.  
  174.    public void changeToParentDirectory() {
  175.       File oldValue = this.getCurrentDirectory();
  176.       this.setCurrentDirectory(this.getFileSystemView().getParentDirectory(oldValue));
  177.    }
  178.  
  179.    public void ensureFileIsVisible(File f) {
  180.       this.getUI().ensureFileIsVisible(f);
  181.    }
  182.  
  183.    protected void fireActionPerformed(String command) {
  184.       Object[] listeners = super.listenerList.getListenerList();
  185.       ActionEvent e = null;
  186.  
  187.       for(int i = listeners.length - 2; i >= 0; i -= 2) {
  188.          Object var10000 = listeners[i];
  189.          Class var10001 = class$java$awt$event$ActionListener;
  190.          if (var10001 == null) {
  191.             try {
  192.                var10001 = Class.forName("java.awt.event.ActionListener");
  193.             } catch (ClassNotFoundException var5) {
  194.                throw new NoClassDefFoundError(((Throwable)var5).getMessage());
  195.             }
  196.  
  197.             class$java$awt$event$ActionListener = var10001;
  198.          }
  199.  
  200.          if (var10000 == var10001) {
  201.             if (e == null) {
  202.                e = new ActionEvent(this, 1001, command);
  203.             }
  204.  
  205.             ((ActionListener)listeners[i + 1]).actionPerformed(e);
  206.          }
  207.       }
  208.  
  209.    }
  210.  
  211.    public FileFilter getAcceptAllFileFilter() {
  212.       FileFilter filter = null;
  213.       if (this.getUI() != null) {
  214.          filter = this.getUI().getAcceptAllFileFilter();
  215.       }
  216.  
  217.       return filter;
  218.    }
  219.  
  220.    public JComponent getAccessory() {
  221.       return this.accessory;
  222.    }
  223.  
  224.    public String getApproveButtonText() {
  225.       return this.approveButtonText;
  226.    }
  227.  
  228.    public String getApproveButtonToolTipText() {
  229.       return this.approveButtonToolTipText;
  230.    }
  231.  
  232.    public FileFilter[] getChoosableFileFilters() {
  233.       FileFilter[] filterArray = new FileFilter[this.filters.size()];
  234.       this.filters.copyInto(filterArray);
  235.       return filterArray;
  236.    }
  237.  
  238.    public File getCurrentDirectory() {
  239.       return this.currentDirectory;
  240.    }
  241.  
  242.    public String getDescription(File f) {
  243.       String description = null;
  244.       if (this.getFileView() != null) {
  245.          description = this.getFileView().getDescription(f);
  246.       }
  247.  
  248.       if (description == null && this.uiFileView != null) {
  249.          description = this.uiFileView.getDescription(f);
  250.       }
  251.  
  252.       return description;
  253.    }
  254.  
  255.    public String getDialogTitle() {
  256.       return this.dialogTitle;
  257.    }
  258.  
  259.    public int getDialogType() {
  260.       return this.dialogType;
  261.    }
  262.  
  263.    public FileFilter getFileFilter() {
  264.       return this.fileFilter;
  265.    }
  266.  
  267.    public int getFileSelectionMode() {
  268.       return this.fileSelectionMode;
  269.    }
  270.  
  271.    public FileSystemView getFileSystemView() {
  272.       return this.fileSystemView;
  273.    }
  274.  
  275.    public FileView getFileView() {
  276.       return this.fileView;
  277.    }
  278.  
  279.    public Icon getIcon(File f) {
  280.       Icon icon = null;
  281.       if (this.getFileView() != null) {
  282.          icon = this.getFileView().getIcon(f);
  283.       }
  284.  
  285.       if (icon == null && this.uiFileView != null) {
  286.          icon = this.uiFileView.getIcon(f);
  287.       }
  288.  
  289.       return icon;
  290.    }
  291.  
  292.    public String getName(File f) {
  293.       String filename = null;
  294.       if (this.getFileView() != null) {
  295.          filename = this.getFileView().getName(f);
  296.       }
  297.  
  298.       if (filename == null && this.uiFileView != null) {
  299.          filename = this.uiFileView.getName(f);
  300.       }
  301.  
  302.       return filename;
  303.    }
  304.  
  305.    public File getSelectedFile() {
  306.       return this.selectedFile;
  307.    }
  308.  
  309.    public File[] getSelectedFiles() {
  310.       return this.selectedFiles == null ? new File[0] : (File[])this.selectedFiles.clone();
  311.    }
  312.  
  313.    public String getTypeDescription(File f) {
  314.       String typeDescription = null;
  315.       if (this.getFileView() != null) {
  316.          typeDescription = this.getFileView().getTypeDescription(f);
  317.       }
  318.  
  319.       if (typeDescription == null && this.uiFileView != null) {
  320.          typeDescription = this.uiFileView.getTypeDescription(f);
  321.       }
  322.  
  323.       return typeDescription;
  324.    }
  325.  
  326.    public FileChooserUI getUI() {
  327.       return (FileChooserUI)super.ui;
  328.    }
  329.  
  330.    public String getUIClassID() {
  331.       return "FileChooserUI";
  332.    }
  333.  
  334.    protected void init() {
  335.       this.setFileSystemView(FileSystemView.getFileSystemView());
  336.       this.updateUI();
  337.    }
  338.  
  339.    public boolean isDirectorySelectionEnabled() {
  340.       return this.fileSelectionMode == 1 || this.fileSelectionMode == 2;
  341.    }
  342.  
  343.    public boolean isFileHidingEnabled() {
  344.       return this.useFileHiding;
  345.    }
  346.  
  347.    public boolean isFileSelectionEnabled() {
  348.       return this.fileSelectionMode == 0 || this.fileSelectionMode == 2;
  349.    }
  350.  
  351.    public boolean isMultiSelectionEnabled() {
  352.       return this.multiSelectionEnabled;
  353.    }
  354.  
  355.    public boolean isTraversable(File f) {
  356.       Boolean traversable = null;
  357.       if (this.getFileView() != null) {
  358.          traversable = this.getFileView().isTraversable(f);
  359.       }
  360.  
  361.       if (traversable == null && this.uiFileView != null) {
  362.          traversable = this.uiFileView.isTraversable(f);
  363.       }
  364.  
  365.       if (traversable == null && f != null) {
  366.          if (f.isDirectory()) {
  367.             traversable = Boolean.TRUE;
  368.          } else {
  369.             traversable = Boolean.FALSE;
  370.          }
  371.       } else if (traversable == null) {
  372.          return false;
  373.       }
  374.  
  375.       return traversable;
  376.    }
  377.  
  378.    public void removeActionListener(ActionListener l) {
  379.       EventListenerList var10000 = super.listenerList;
  380.       Class var10001 = class$java$awt$event$ActionListener;
  381.       if (var10001 == null) {
  382.          try {
  383.             var10001 = Class.forName("java.awt.event.ActionListener");
  384.          } catch (ClassNotFoundException var2) {
  385.             throw new NoClassDefFoundError(((Throwable)var2).getMessage());
  386.          }
  387.  
  388.          class$java$awt$event$ActionListener = var10001;
  389.       }
  390.  
  391.       var10000.remove(var10001, l);
  392.    }
  393.  
  394.    public boolean removeChoosableFileFilter(FileFilter f) {
  395.       if (this.filters.contains(f)) {
  396.          FileFilter[] oldValue = this.getChoosableFileFilters();
  397.          this.filters.removeElement(f);
  398.          ((JComponent)this).firePropertyChange("ChoosableFileFilterChangedProperty", oldValue, this.getChoosableFileFilters());
  399.          return true;
  400.       } else {
  401.          return false;
  402.       }
  403.    }
  404.  
  405.    public void rescanCurrentDirectory() {
  406.       this.getUI().rescanCurrentDirectory();
  407.    }
  408.  
  409.    public void resetChoosableFileFilters() {
  410.       FileFilter[] oldValue = this.getChoosableFileFilters();
  411.       this.filters.removeAllElements();
  412.       this.filters.addElement(this.getAcceptAllFileFilter());
  413.       ((JComponent)this).firePropertyChange("ChoosableFileFilterChangedProperty", oldValue, this.getChoosableFileFilters());
  414.    }
  415.  
  416.    public void setAccessory(JComponent newAccessory) {
  417.       JComponent oldValue = this.accessory;
  418.       this.accessory = newAccessory;
  419.       ((JComponent)this).firePropertyChange("AccessoryChangedProperty", oldValue, this.accessory);
  420.    }
  421.  
  422.    public void setApproveButtonText(String approveButtonText) {
  423.       if (this.approveButtonText != approveButtonText) {
  424.          String oldValue = this.approveButtonText;
  425.          this.approveButtonText = approveButtonText;
  426.          this.setDialogType(2);
  427.          ((JComponent)this).firePropertyChange("ApproveButtonTextChangedProperty", oldValue, approveButtonText);
  428.       }
  429.    }
  430.  
  431.    public void setApproveButtonToolTipText(String toolTipText) {
  432.       if (this.approveButtonToolTipText != toolTipText) {
  433.          String oldValue = this.approveButtonToolTipText;
  434.          this.approveButtonToolTipText = toolTipText;
  435.          this.setDialogType(2);
  436.          ((JComponent)this).firePropertyChange("ApproveButtonToolTipTextChangedProperty", oldValue, this.approveButtonToolTipText);
  437.       }
  438.    }
  439.  
  440.    public void setCurrentDirectory(File dir) {
  441.       if (this.currentDirectory != dir || dir == null) {
  442.          File oldValue = this.currentDirectory;
  443.          if (dir == null) {
  444.             this.currentDirectory = this.getFileSystemView().createFileObject(System.getProperty("user.home"));
  445.          } else {
  446.             for(File prev = null; !this.isTraversable(dir) && prev != dir && !this.getFileSystemView().isRoot(dir); dir = this.getFileSystemView().getParentDirectory(dir)) {
  447.                prev = dir;
  448.             }
  449.  
  450.             this.currentDirectory = dir;
  451.          }
  452.  
  453.          ((JComponent)this).firePropertyChange("directoryChanged", oldValue, this.currentDirectory);
  454.       }
  455.    }
  456.  
  457.    public void setDialogTitle(String dialogTitle) {
  458.       this.dialogTitle = dialogTitle;
  459.       if (this.dialog != null) {
  460.          this.dialog.setTitle(dialogTitle);
  461.       }
  462.  
  463.    }
  464.  
  465.    public void setDialogType(int dialogType) {
  466.       if (this.dialogType != dialogType) {
  467.          if (dialogType != 0 && dialogType != 1 && dialogType != 2) {
  468.             throw new IllegalArgumentException("Incorrect Dialog Type: " + dialogType);
  469.          } else {
  470.             int oldValue = this.dialogType;
  471.             this.dialogType = dialogType;
  472.             if (dialogType == 0 || dialogType == 1) {
  473.                this.setApproveButtonText((String)null);
  474.             }
  475.  
  476.             ((JComponent)this).firePropertyChange("DialogTypeChangedProperty", oldValue, dialogType);
  477.          }
  478.       }
  479.    }
  480.  
  481.    public void setFileFilter(FileFilter filter) {
  482.       FileFilter oldValue = this.fileFilter;
  483.       this.fileFilter = filter;
  484.       ((JComponent)this).firePropertyChange("fileFilterChanged", oldValue, this.fileFilter);
  485.    }
  486.  
  487.    public void setFileHidingEnabled(boolean b) {
  488.       boolean oldValue = this.useFileHiding;
  489.       this.useFileHiding = b;
  490.       ((JComponent)this).firePropertyChange("FileHidingChanged", oldValue, this.useFileHiding);
  491.    }
  492.  
  493.    public void setFileSelectionMode(int mode) {
  494.       if (this.fileSelectionMode != mode) {
  495.          int oldValue = this.fileSelectionMode;
  496.          this.fileSelectionMode = mode;
  497.          ((JComponent)this).firePropertyChange("fileSelectionChanged", oldValue, this.fileSelectionMode);
  498.       }
  499.    }
  500.  
  501.    public void setFileSystemView(FileSystemView fileSystemView) {
  502.       FileSystemView oldValue = this.fileSystemView;
  503.       this.fileSystemView = fileSystemView;
  504.       ((JComponent)this).firePropertyChange("FileSystemViewChanged", oldValue, fileSystemView);
  505.    }
  506.  
  507.    public void setFileView(FileView fileView) {
  508.       FileView oldValue = this.fileView;
  509.       this.fileView = fileView;
  510.       ((JComponent)this).firePropertyChange("fileViewChanged", oldValue, fileView);
  511.    }
  512.  
  513.    public void setMultiSelectionEnabled(boolean b) {
  514.       if (this.multiSelectionEnabled != b) {
  515.          boolean oldValue = this.multiSelectionEnabled;
  516.          this.multiSelectionEnabled = b;
  517.          ((JComponent)this).firePropertyChange("fileFilterChanged", oldValue, this.multiSelectionEnabled);
  518.       }
  519.    }
  520.  
  521.    public void setSelectedFile(File selectedFile) {
  522.       File oldValue = this.selectedFile;
  523.       this.selectedFile = selectedFile;
  524.       this.ensureFileIsVisible(selectedFile);
  525.       ((JComponent)this).firePropertyChange("ApproveSelection", oldValue, this.selectedFile);
  526.    }
  527.  
  528.    public void setSelectedFiles(File[] selectedFiles) {
  529.       this.selectedFiles = selectedFiles;
  530.    }
  531.  
  532.    public int showDialog(Component parent, String approveButtonText) {
  533.       if (approveButtonText != null) {
  534.          this.setApproveButtonText(approveButtonText);
  535.       }
  536.  
  537.       Frame var10000;
  538.       if (parent instanceof Frame) {
  539.          var10000 = (Frame)parent;
  540.       } else {
  541.          Class var6 = class$java$awt$Frame;
  542.          if (var6 == null) {
  543.             try {
  544.                var6 = Class.forName("java.awt.Frame");
  545.             } catch (ClassNotFoundException var5) {
  546.                throw new NoClassDefFoundError(((Throwable)var5).getMessage());
  547.             }
  548.  
  549.             class$java$awt$Frame = var6;
  550.          }
  551.  
  552.          var10000 = (Frame)SwingUtilities.getAncestorOfClass(var6, parent);
  553.       }
  554.  
  555.       Frame frame = var10000;
  556.       this.dialog = new JDialog(frame, this.getDialogTitle(), true);
  557.       Container contentPane = this.dialog.getContentPane();
  558.       contentPane.setLayout(new BorderLayout());
  559.       contentPane.add(this, "Center");
  560.       this.dialog.pack();
  561.       this.dialog.setLocationRelativeTo(parent);
  562.       this.dialog.show();
  563.       return this.returnValue;
  564.    }
  565.  
  566.    public int showOpenDialog(Component parent) {
  567.       this.setDialogType(0);
  568.       return this.showDialog(parent, (String)null);
  569.    }
  570.  
  571.    public int showSaveDialog(Component parent) {
  572.       this.setDialogType(1);
  573.       return this.showDialog(parent, (String)null);
  574.    }
  575.  
  576.    public void updateUI() {
  577.       ((JComponent)this).setUI((FileChooserUI)UIManager.getUI(this));
  578.       this.uiFileView = this.getUI().getFileView();
  579.       boolean useAcceptAllFileFilter = this.removeChoosableFileFilter(this.getAcceptAllFileFilter());
  580.       if (useAcceptAllFileFilter) {
  581.          this.addChoosableFileFilter(this.getAcceptAllFileFilter());
  582.       }
  583.  
  584.    }
  585. }
  586.